#include "mainwidget.h"
#include "./ui_mainwidget.h"
#include <QHBoxLayout>
#include <QVBoxLayout>

#include "sessionfriendarea.h"
#include "debug.h"

MainWidget* MainWidget::instance = nullptr;

MainWidget* MainWidget::getInstance()
{
    if(instance == nullptr)
    {
        instance = new MainWidget();
    }
    return instance;
}

MainWidget::MainWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MainWidget)
{
    ui->setupUi(this);

    this->setWindowTitle("我的聊天室");
    this->setWindowIcon(QIcon(":/recource/image/weixin.jpg"));

    //初始化主窗口的样式布局
    initMainWindow();
    //初始化左侧窗口布局
    initLeftWindow();
    //初始化中间窗口布局
    initMidWindow();
    //初始化右侧窗口布局
    initRightWindow();
    //初始化信号槽
    initSignalSlot();
}

MainWidget::~MainWidget()
{
    delete ui;
}

void MainWidget::initMainWindow()
{
    QHBoxLayout* layout = new QHBoxLayout();
    //Spacing 就是 layout 内部元素之间的间隔距离，设为0 ，就是紧挨着
    layout->setSpacing(0);
    //layout 里面的元素距离四个边界的距离
    layout->setContentsMargins(0,0,0,0);
    this->setLayout(layout);

    windowLeft = new QWidget();
    windowMid = new QWidget();
    windowRight = new QWidget();

    windowLeft->setFixedWidth(70);
    windowMid->setFixedWidth(310);
    windowRight->setMinimumWidth(600);

    windowLeft->setStyleSheet("QWidget { background-color: rgb(46,46,46);}");
    windowMid->setStyleSheet("QWidget { background-color: rgb(247,247,247);}");
    windowRight->setStyleSheet("QWidget { background-color: rgb(245,245,245);}");

    layout->addWidget(windowLeft);
    layout->addWidget(windowMid);
    layout->addWidget(windowRight);
}

void MainWidget::initLeftWindow()
{
    QVBoxLayout* layout = new QVBoxLayout();
    layout->setSpacing(20);
    layout->setContentsMargins(0,50,0,0);
    windowLeft->setLayout(layout);

    //添加用户头像
    userAvatar = new QPushButton();
    userAvatar->setFixedSize(45,45);
    userAvatar->setIconSize(QSize(45,45) );
    userAvatar->setIcon(QIcon(":/recource/image/yang.jpg"));
    userAvatar->setStyleSheet("QPushButton { background-color: transparent;}");
    layout->addWidget(userAvatar,1,Qt::AlignTop | Qt::AlignHCenter);

    //添加会话标签页按钮
    sessionTabBtn = new QPushButton();
    sessionTabBtn->setFixedSize(45,45);
    sessionTabBtn->setIconSize(QSize(30,30) );
    sessionTabBtn->setIcon(QIcon(":/recource/image/session.png"));
    sessionTabBtn->setStyleSheet("QPushButton { background-color: transparent;}");
    layout->addWidget(sessionTabBtn,1,Qt::AlignTop | Qt::AlignHCenter);

    //添加好友标签页按钮
    friendTabBtn = new QPushButton();
    friendTabBtn->setFixedSize(45,45);
    friendTabBtn->setIconSize(QSize(30,30) );
    friendTabBtn->setIcon(QIcon(":/recource/image/friend.png"));
    friendTabBtn->setStyleSheet("QPushButton { background-color: transparent;}");
    layout->addWidget( friendTabBtn,1,Qt::AlignTop | Qt::AlignHCenter);

    //添加好友申请标签页按钮
    applyTabBtn = new QPushButton();
    applyTabBtn->setFixedSize(45,45);
    applyTabBtn->setIconSize(QSize(30,30) );
    applyTabBtn->setIcon(QIcon(":/recource/image/apply.png"));
    applyTabBtn->setStyleSheet("QPushButton { background-color: transparent;}");
    layout->addWidget(applyTabBtn,1,Qt::AlignTop | Qt::AlignHCenter);

    layout->addStretch(20);


}

void MainWidget::initMidWindow()
{
    QGridLayout* layout = new QGridLayout();
    // 距离上方有 20px 的距离，另外三个方向都不要边距
    layout->setContentsMargins(0,20,0,0);
    layout->setSpacing(0);
    layout->setVerticalSpacing(10);
    windowMid->setLayout(layout);

    searchEdit = new QLineEdit();
    searchEdit->setFixedHeight(30);
    searchEdit->setPlaceholderText("搜索");
    searchEdit->setStyleSheet("QLineEdit { border-radius: 5px; background-color: rgb(226, 226, 226); padding-left: 5px;}");

    addFriendBtn = new QPushButton();
    addFriendBtn->setFixedSize(30,30);
    addFriendBtn->setIcon(QIcon(":/recource/image/cross.png"));
    QString style = "QPushButton { border-radius: 5px; background-color: rgb(226, 226, 226);  }";
    style += " QPushButton:pressed { background-color: rgb(240,240,240);}";
    addFriendBtn->setStyleSheet(style);

    SessionFriendArea* sessionFriendArea = new SessionFriendArea();

    //为了更加灵活的控制边距，只影响搜索框按钮这一行，不影响下方列表这一行
    // 创建空白的 widget 填充到布局管理器中
    QWidget* spacer1 = new QWidget();
    spacer1->setFixedWidth(10);
    QWidget* spacer2 = new QWidget();
    spacer2->setFixedWidth(10);
    QWidget* spacer3 = new QWidget();
    spacer3->setFixedWidth(10);

    layout->addWidget(spacer1,0,0);
    layout->addWidget(searchEdit,0,1);
    layout->addWidget(spacer2,0,2);
    layout->addWidget(addFriendBtn,0,3);
    layout->addWidget(spacer3,0,4);
    layout->addWidget(sessionFriendArea,1,0,1,5);

}

void MainWidget::initRightWindow()
{
    //1.创建右侧窗口的布局管理器
    QVBoxLayout* vlayout = new QVBoxLayout();
    vlayout->setSpacing(0);
    vlayout->setContentsMargins(0,0,0,0);
    vlayout->setAlignment(Qt::AlignTop);
    windowRight->setLayout(vlayout);

    //2.创建上方标题栏
    QWidget* titleWidget = new QWidget();
    titleWidget->setFixedHeight(62);
    titleWidget->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    titleWidget->setObjectName("titleWidget");
    titleWidget->setStyleSheet("#titleWidget { border-bottom: 1px solid rgb(230,230,230); border-left:1px solid rgb(230,230,230);}");
    vlayout->addWidget(titleWidget);

    //3.给标题栏添加标题label 和一个按钮
    QHBoxLayout* hlayout = new QHBoxLayout();
    hlayout->setSpacing(0);
    //使标题的 label 和 按钮距离左右两侧的边界，有点边距
    hlayout->setContentsMargins(10,0,10,0);
    titleWidget->setLayout(hlayout);

    QLabel* sessionTitleLabel = new QLabel();
    sessionTitleLabel->setStyleSheet("QLabel { font-size: 22px;border-bottom: 1px solid rgb(230,230,230);}");

#if TEST_UI
    //为了测试界面临时增加，实际这里的内容，应该是使用从服务器获取的数据啦设置
    sessionTitleLabel->setText("这是会话标题");
#endif
    hlayout->addWidget(sessionTitleLabel);

    QPushButton* extraBtn = new QPushButton();
    extraBtn->setFixedSize(30,30);
    extraBtn->setIconSize(QSize(30,30));
    extraBtn->setIcon(QIcon(":/recource/image/more.png"));
    extraBtn->setStyleSheet("QPushButton { border:none; background-color: rgb(245,245,245);} QPushButton:pressed { background-color:rgb(220,220,220);}");
    hlayout->addWidget(extraBtn);

    //4.添加消息展示区
    messageShowArea = new MessageShowArea();
    vlayout->addWidget(messageShowArea);

    //5. 添加消息编辑区
    messageEditArea = new MessageEditArea();
    // 确保消息编辑区，处于窗口的下方
    vlayout->addWidget(messageEditArea,0,Qt::AlignBottom);
}


void MainWidget::initSignalSlot()
{
    //链接信号槽，处理标签页切换的问题
    connect(sessionTabBtn,&QPushButton::clicked,this, &MainWidget::switchTabToSession);
    connect(friendTabBtn,&QPushButton::clicked,this, &MainWidget::switchTabToFriend);
    connect(applyTabBtn,&QPushButton::clicked,this, &MainWidget::switchTabToApply);
}

void MainWidget::switchTabToSession()
{
    //1. 记录当前切换到了那个标签页
    activeTab = SESSION_LIST;
    //2. 调整图标显示情况，把会话的按钮图标设为 active ，另外了两个图标设为 inactive
    sessionTabBtn->setIcon(QIcon(":/recource/image/session.png"));
    friendTabBtn->setIcon(QIcon(":/recource/image/friend.png"));
    applyTabBtn->setIcon(QIcon(":/recource/image/apply.png"));

    //3.在主窗口中，加载出会话列表数据
    this->loadSessionList();
}

void MainWidget::switchTabToFriend()
{
    //1. 记录当前切换到了那个标签页
    activeTab = FRIEND_LIST;
    //2. 调整图标显示情况，把会话的按钮图标设为 active ，另外了两个图标设为 inactive
    friendTabBtn->setIcon(QIcon(":/recource/image/friend1.png"));
    sessionTabBtn->setIcon(QIcon(":/recource/image/session1.png"));
    applyTabBtn->setIcon(QIcon(":/recource/image/apply.png"));

    //3.在主窗口中，加载出会话列表数据
    this->loadFriendList();
}

void MainWidget::switchTabToApply()
{
    //1. 记录当前切换到了那个标签页
    activeTab =APPYL_LIST;
    //2. 调整图标显示情况，把会话的按钮图标设为 active ，另外了两个图标设为 inactive
    applyTabBtn->setIcon(QIcon(":/recource/image/apply1.png"));
    friendTabBtn->setIcon(QIcon(":/recource/image/friend.png"));
    sessionTabBtn->setIcon(QIcon(":/recource/image/session1.png"));


    //3.在主窗口中，加载出会话列表数据
    this->loadApplyList();
}

//加载会话列表
void MainWidget::loadSessionList()
{
     //TODO
}

void MainWidget::loadFriendList()
{
    //TODO
}

void MainWidget::loadApplyList()
{
    //TODO
}
